package Hot100.Medium.GreedyAndDP.Greedy;

public class LC581 {
    public static void main(String[] args) {

    }

    public int findUnsortedSubarray(int[] nums){
        int begin = 0, end = -1;
        int max = nums[0];
        int min = nums[nums.length - 1];

        for(int i = 0; i < nums.length; i++){
            // 从左往右寻找end，也就是非递增区间的右端点
            if(nums[i] < max){
                end = i;
            } else {
                max = nums[i];
            }
            //从右往左寻找start也就是非递增区间的左端点
            if(nums[nums.length - i - 1] > min){
                begin = nums.length - i - 1;
            } else {
                min = nums[nums.length - i - 1];
            }
        }
        return end - begin + 1;
    }

}
